<?php

namespace App\Http\Controllers\Api\V1\Admin;


use App\Http\Resources\BooksResource;
use App\Models\Books;
use App\Models\BookType;
use App\Models\Category;
use App\Models\New_Category;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class BooksController extends BaseController
{
    public function index(Books $books, Request $request)
    {
        $query = $books->newQuery();
        $modelId = $request->input('module_id', null);
        if ($modelId) {
            $query->where('module_id', $modelId);
//            $query->whereNotIn('status', [0, 1]);
            if ($request->has('type_id') && is_array($request->type_id)) {
                if (count($request->type_id) == 1) {
                    $query->where('type_id', $request->type_id[0]);
                } elseif (count($request->type_id) > 1) {
                    $query->where(function ($querys) use ($request) {
                        foreach ($request->type_id as $typeid) {
                            $querys->orwhere('type_id', 'like', '%' . $typeid . '%');
                        }
                    });
                }
            }
            //排序
            switch ($request->order) {
                case 1:
                    $query->orderBy('view_num', 'desc');
                    break;//点击数
                case 2:
                    $query->orderBy('like_num', 'desc');
                    break;//点赞数
                case 3:
                    $query->orderBy('comment_num', 'desc');
                    break;//评论数
                case 4:
                    $query->orderBy('share_num', 'desc');
                    break;//分享数
                case 5:
                    $query->orderBy('collect_num', 'desc');
                    break;//收藏数
                case 6:
                    $query->orderBy('is_top', 'desc');
                    break;//置顶大小
                default:
                    $query->orderBy('updated_at', 'desc');//不传按更新时间排序
            }
        }
        $list = $query->with('user')->paginate();
        foreach ($list as &$value) {
            $temp = BookType::whereIn('id', explode(',', $value->type_id))->pluck('name')->toArray();
            $value['type_name'] = implode("-", $temp);
            $value['module_name'] = Books::MODULE_ID[$value['module_id']];
        }
        return $this->success('成功', $list);
//
//        $category_id = request()->input('category_id');
//        $keyword = request()->input('keyword');
//        $status = request()->input('status');
//        $list = News::where(function ($query) use ($keyword, $status, $category_id) {
//            if ($status) {
//                $query->where('status', $status);
//            } else {
//                $query->whereIn('status', [1, 2]);
//            }
//            if ($keyword) {
//                $query->where('title', '%' . $keyword . '%');
//            }
//            if ($category_id) {
//                $query->whereIn('category_id', $category_id);
//            }
//        })->orderBy('id', 'DESC')
//            ->paginate($this->page_size);
//        foreach ($list as &$value) {
//            $value['imgs'] = json_decode($value['imgs'], true);
//            $value['body'] = Str::limit($value['body'], 20, '...');;
//            $value['videos'] = json_decode($value['videos'], true);
//            $value['category_name'] = New_Category::where('id', $value['category_id'])->value('name');
//        }
//        return $this->success('成功', $list);
    }

    public function store(Request $request)
    {

        if (empty($request->name)) {
            return $this->error('书名不能为空');
        }
        if (!in_array($request->module_id, array_keys(Books::MODULE_ID))) {
            return $this->error('非法模块');
        }
        $model = new Books();
        $model->name = $request->name;
        if (!empty($request->images) && is_array($request->images) && count($request->images)) {
            $model->cover = implode(',', $request->images);//封面
        } else {
            $model->cover = '';
        }
        if (mb_strlen($request->intro) > 5000) {
            return $this->error('添加失败,简介字数太多');
        }
        $model->intro = $request->intro;//简介
        $model->user_id = $this->getUserId();
        if (!is_array($request->type_id)) {
            $typeid = [];
            array_push($typeid, $request->type_id);;
        } else {
            $typeid = $request->type_id;
        }
        $model->type_id = implode(',', $typeid);
        $model->module_id = $request->module_id;
        $result = $model->save();
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->error('添加失败');
        }
    }

    public function update(Request $request, $book)
    {
        if (empty($request->name)) {
            return $this->error('书名不能为空');
        }
        if (!in_array($request->module_id, array_keys(Books::MODULE_ID))) {
            return $this->error('非法模块');
        }
        $model = Books::find($book);
        $model->name = $request->name;
        if (!empty($request->images) && is_array($request->images) && count($request->images)) {
            $model->cover = implode(',', $request->images);//封面
        } else {
            $model->cover = '';
        }
        $model->intro = $request->intro;//简介
        $model->user_id = $this->getUserId();
        if (!is_array($request->type_id)) {
            $typeid = [];
            array_push($typeid, $request->type_id);;
        } else {
            $typeid = $request->type_id;
        }
        $model->type_id = implode(',', $typeid);
        $model->module_id = $request->module_id;
        $result = $model->save();
        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }

    public function destroy($id)
    {
        $result = Books::where('id', $id)->update(['status' => 0]);
        if ($result) {
            return $this->success('删除成功', $result);
        } else {
            return $this->error('删除失败');
        }
    }

    public function batchDisable(Request $request)
    {
        $input = $request->toArray();
        if (!in_array($input['status'], [0, 1, 2])) {
            return $this->error('状态不正确');
        }
        $result = Books::whereIn('id', $input['selection'])->update(['status' => $input['status']]);
        if ($result) {
            return $this->success('保存成功');
        } else {
            return $this->error('保存失败');
        }
    }

    public function getModels()
    {
        return $this->success('成功', Books::MODULE);
    }
}
